//腾讯地图
var QQMapWX = require('../../libs/qqmap/qqmap-wx-jssdk.min.js');
var qqmapsdk;
var address;
//时间选择
var dateTimePicker = require('../../utils/dateTimePicker.js');
//坐标转换
var coordtransform = require('../../utils/coordtransform.js');
// 获取全局应用程序实例对象
const app = getApp()

Page({
  data: {
    hasLocation:'',
    longitude:'',
    latitude:'',
    address:{},
    car_info:{},
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray1: null,//时间
    dateTime1: null,
    showModalStatus: false,  //模态框
    remark:''//备注
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'E2UBZ-74OKK-V6LJ3-AUJ3U-IYHVE-A5FNH'
    });
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res.latitude + '---' + res.longitude);
        that.setData({
          latitude: res.latitude
        });
        that.setData({
          longitude: res.longitude
        });
        var wgs84togcj02 = coordtransform.wgs84togcj02(res.longitude, res.latitude);
        //根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: wgs84togcj02[1],
            longitude: wgs84togcj02[0]
          },
          success: function (addressRes) {
            console.log(addressRes)
            address = addressRes.result.address + addressRes.result.formatted_addresses.recommend;
            that.setData({
             address
            })
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
      }
    })

    //时间选择
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });

    //车辆信息 
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
    };
    app.network.POST(
      'https://www.ausck.com/api/get_car_info',
      {
        params: params,
        success: function (res) {
          console.log(res.data.data)
          //拿到解密后的数据，进行代码逻辑  
          that.setData({
            car_info: res.data.data
          });
        },
        fail: function () {
          //失败后的逻辑  

        },
      });
 
    if (options.id){
      this.setData({
        project_id: options.id
      })
      app.network.POST(
        'https://www.ausck.com/api/project_info',
        {
          params: {id:options.id},
          success: function (res) {
            console.log(res.data.data)
            //拿到解密后的数据，进行代码逻辑
            that.setData({
              project: res.data.data
            })
          },
          fail: function () {
            //失败后的逻辑  

          },
        });
    }
    
  },
  toProject:function(){
    wx.navigateTo({
      url: '/pages/project/project',
    })
  },
  toVehicleInfo: function () {
    wx.navigateTo({
      url: '/pages/vehicle-information/vehicle-information',
    })
  },
  //时间选择
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr
    });
  },

  //模态框
  powerDrawer: function (e) {
    console.log(e)
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  formSubmit:function(e){
    this.setData({
      remark: e.detail.value.remark
    })
  },
 
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },

  payTap:function(){
    //车辆信息 
    var that = this;
    //转化时间格式
    var dateTimeArray1 = that.data.dateTimeArray1;
    var dateTime1 = that.data.dateTime1;
    var service_time = dateTimeArray1[0][dateTime1[0]] + '-' + dateTimeArray1[1][dateTime1[1]] + '-' + dateTimeArray1[2][dateTime1[2]] + ' ' + dateTimeArray1[3][dateTime1[3]] + ':' + dateTimeArray1[4][dateTime1[4]];
    var params = {
      openid: app.globalData.info.openid,
      address: that.data.address,
      project_id: that.data.project_id,
      remark: that.data.remark,
      service_time: service_time
    };
    app.network.POST(
      'https://www.ausck.com/api/add_order',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑
          console.log(res.data.config)
          var info = res.data.config;
          
          wx.requestPayment({
            'timeStamp': info.timeStamp,
            'nonceStr': info.nonceStr,
            'package': info.package,
            'signType': 'MD5',
            'paySign': info.paySign,
            'success': function (res) {
              wx.navigateTo({
                url: '/pages/myorder/myorder',
              })
            },
            'fail': function (res) {
            } 
          })
        },
        fail: function () {
          //失败后的逻辑  

        },
      });
   
  }
})
